package com.chat.datea.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chat.datea.R;
import com.chat.datea.bean.SearchTopEntity;

import java.util.ArrayList;

/**
 * Created by Shura on 15/10/28.
 */
public class SearchListViewAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<SearchTopEntity.ResultEntity> mResultEntities;

    public SearchListViewAdapter(Context context, ArrayList<SearchTopEntity.ResultEntity> resultEntities) {
        this.mContext = context;
        this.mResultEntities = resultEntities;
    }


    @Override
    public int getCount() {
        if (mResultEntities != null && mResultEntities.size() > 0)
            return mResultEntities.size();
        else
            return 0;
    }

    @Override
    public SearchTopEntity.ResultEntity getItem(int position) {
        if (mResultEntities != null && mResultEntities.size() > 0)
            return mResultEntities.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            // 初始化缓存组件
            viewHolder = new ViewHolder();
            // 通过布局加载器加载行布局
            convertView = LayoutInflater.from(mContext).inflate(R.layout.search_listview_item, null);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.search_listView_item_text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(mResultEntities.get(position).getWord());

        return convertView;
    }

    public void remove(SearchTopEntity.ResultEntity resultEntity) {
        mResultEntities.remove(resultEntity);
        notifyDataSetChanged();
    }


    private class ViewHolder {
        private TextView textView;
    }
}
